const db = wx.cloud.database()
const teamlistCollection = db.collection('teamlist')
const applicationCollection = db.collection('applications')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    gradelist:['大一','大二','大三','大四','研一','研二','研三'],
    gradetype:'',
    major:'',
    wxid:'',
    awards:'',
    advantage:'',
    teamid:'',
    currentteam:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      teamid: options.teamid,
      teamtype: options.teamtype
    })
    console.log(this.data.teamtype)
  },
  bindPickerChange: function (e) {
    let i = e.detail.value
    this.setData({
      gradetype: this.data.gradelist[i]
    })
  },
  formSubmit: function (e) {
    let data = e.detail.value
    let datavalid = this.checkdata(data)
    if (!datavalid) {
      return
    }
    let userInfo = wx.getStorageSync('userInfo')
    Object.assign(
      data,
      { teamid: this.data.teamid},
      { nickname: userInfo.nickname },
      { date: Date.now() },
      { approval: 0 }
    )
    teamlistCollection.where({
      _id: this.data.teamid
    }).get({
      success:function(res){
      },
      fail: function(){
        wx.showToast({
          title: '获取数据失败，请稍后再试',
          icon: 'none'
        });
      }
    })
    applicationCollection.add({
      data: data,
      success: (res) => {
        console.log(res)
        wx.showToast({
          title: 'title',
          success: (res)=>{
            wx.navigateBack({
              delta: 1,
            })
          }
        })
        

      },
      fail:(res)=>{

      }
    })
  },
  formReset: function () {
    this.setData({
      gradetype:'',
      major:'',
      wxid:'',
      awards:'',
      advantage:''
    })
  },
  checkdata(data) {
    console.log(data)
    if (
      !data.advantages ||
      !data.awards ||
      !data.wxid
    ) {
      wx.showToast({
        title: '申请失败，请将信息填写完整！',
        icon: 'none',
        image: '',
        duration: 2000,
        mask: false,
        success: (result) => {},
        fail: () => {},
        complete: () => {},
      })
      return false
    }else{
      return true
    }
  }
})